/**
 * @a https://leetcode.cn/problems/count-and-say/
 */

#include "common.h"
class Solution
{
public:
    string countAndSay(int n)
    {
        if(n == 1) return "1";
        string str = "1";
        while(--n) // n - 1 次
        {
            int sz = str.size();
            string next;
            int left = 0,right = 0;
            while(right < sz)
            {
                while(right < sz && str[left] == str[right]) right++;
                next += to_string(right - left) + str[left];
                left = right;
            }
            str = next;
        }
        return str;
    }
};